﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace Donal.Xms.Common
{
    public static class IniOperation
    {
        public static void DelKey(string section, string key, string fileName)
        {
            WritePrivateProfileString(section, key, null, fileName);
        }

        public static void DelSection(string section, string fileName)
        {
            WritePrivateProfileString(section, null, null, fileName);
        }

        public static int GetInt(string section, string key, int def, string fileName)
        {
            return GetPrivateProfileInt(section, key, def, fileName);
        }

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath);
        public static string GetString(string section, string key, string def, string fileName)
        {
            StringBuilder retVal = new StringBuilder(0x400);
            GetPrivateProfileString(section, key, def, retVal, 0x400, fileName);
            return retVal.ToString();
        }

        public static string GetString(string section, string key, string def, string fileName, int size)
        {
            StringBuilder retVal = new StringBuilder();
            GetPrivateProfileString(section, key, def, retVal, size, fileName);
            return retVal.ToString();
        }

        public static void WriteInt(string section, string key, int iVal, string fileName)
        {
            WritePrivateProfileString(section, key, iVal.ToString(), fileName);
        }

        [DllImport("kernel32")]
        private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);
        public static void WriteString(string section, string key, string strVal, string fileName)
        {
            WritePrivateProfileString(section, key, strVal, fileName);
        }
    }
}
